package com.itisyue.fun.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.UUID;

public class EncodeUtil {
    /**
     * des 加密
     * @param plainText
     * @param desKeyParameter 加密秘钥
     * @return 二进制字节数组
     * @throws Exception
     */
    public static byte[] desEncrypt(byte[] plainText, String desKeyParameter) throws Exception {
        SecureRandom sr = new SecureRandom();
        byte rawKeyData[] = desKeyParameter.getBytes();
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key, sr);
        byte data[] = plainText;
        byte encryptedData[] = cipher.doFinal(data);
        return encryptedData;
    }

    /**
     * des 解密
     * @param encryptText
     * @param desKeyParameter 解密秘钥
     * @return 二进制字节数组
     * @throws Exception
     */
    public static byte[] desDecrypt(byte[] encryptText, String desKeyParameter) throws Exception {
        SecureRandom sr = new SecureRandom();
        byte rawKeyData[] = desKeyParameter.getBytes();
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key, sr);
        byte encryptedData[] = encryptText;
        byte decryptedData[] = cipher.doFinal(encryptedData);
        return decryptedData;
    }


    public static void test01(String text){
        try {
            /**
             * static Base64.Encoder getEncoder()
             * 返回一个 Base64.Encoder ，编码使用基本型 base64 编码方案。
             */
            String base64encodedString = Base64.getEncoder().encodeToString(text.getBytes("utf-8"));
            System.out.println("Base64 编码字符串 (基本) :" + base64encodedString);

            /**
             * static Base64.Decoder getDecoder()
             * 返回一个 Base64.Decoder ，解码使用基本型 base64 编码方案。
             */
            byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
            System.out.println("原始字符串: " + new String(base64decodedBytes, "utf-8"));
            System.out.println("");



            /**
             * static Base64.Encoder getUrlEncoder()
             * 返回一个 Base64.Encoder ，编码使用 URL 和文件名安全型 base64 编码方案。
             */
            base64encodedString = Base64.getUrlEncoder().encodeToString(text.getBytes("utf-8"));
            System.out.println("Base64 编码字符串 (URL) :" + base64encodedString);

            /**
             * static Base64.Decoder getUrlDecoder()
             * 返回一个 Base64.Decoder ，解码使用 URL 和文件名安全型 base64 编码方案。
             */
            byte[] base64decodedBytes2 = Base64.getUrlDecoder().decode(base64encodedString);
            System.out.println("原始字符串: " + new String(base64decodedBytes2, "utf-8"));
            System.out.println("");

            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 10; ++i) {
                stringBuilder.append(UUID.randomUUID().toString());
            }

            byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8");
            String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes);
            System.out.println("Base64 编码字符串 (MIME) :" + mimeEncodedString);

        }catch(UnsupportedEncodingException e){
            System.out.println("Error :" + e.getMessage());
        }
    }
    public static void main(String[] args) throws Exception {
        String original = "/data/media/handWrite/2022/07/28/123456789012345.jpg";
        //String encoded = URLEncoder.encode(original, "UTF-8");
        //System.out.println(encoded);
        //String decode = URLDecoder.decode(encoded, "UTF-8");
        //System.out.println(decode);

        //test01(original);
        byte[] bytes = desEncrypt(original.getBytes("utf-8"),"1qazxsdc");
        String encode = new String(bytes);

        System.out.println(encode);

        byte[] bytes1 = desDecrypt(encode.getBytes(), "1qazxsdc");
        String decode = new String(bytes1);
        System.out.println(decode);

    }
}
